package top.jnaw.jee.pm.controller;

import com.jfinal.core.Controller;
import com.jfinal.kit.PropKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import top.jnaw.jee.pm.model.Projects;
import top.jnaw.jee.utils.Https;

public class ProjectsController extends Controller {


  /**
   * 获取项目基本信息
   */
//  @RequiresAuthentication
//  @RequiresPermissions({"projects:get"})
  public void get() {

    final int pid = getParaToInt("pid", -1);
    Record projects = new Record();
    if (pid > -1) {
      projects = Projects.dao.findByPid(pid);

    }

    if (projects != null) {
      renderJson(Https.success("查询成功", projects));
    } else {
      renderJson(Https.failure("查询失败", ""));
    }
  }

  /**
   * 删除项目
   */
  public void rm() {

    final int pid = getParaToInt("pid", -1);
    boolean result = false;
    if (pid > -1) {
      Projects projects = Projects.dao.findById(pid);
      if (projects != null) {
        result = projects.delete();
      }
    }

    if (result) {
      renderJson(Https.success("删除项目成功", ""));
    } else {
      renderJson(Https.failure("删除项目失败", ""));
    }
  }

  /**
   * 添加项目
   */
  public void add() {
    String name = getPara("name", "");
    String lead_time = getPara("lead_time", "");
    String about = getPara("about", "");
    final int uid = getParaToInt("uid", -1);
    final int fzrid = getParaToInt("fzrid", -1);

    boolean result = false;
    if (StrKit.notBlank(name) && uid > -1) {
      Projects projects = new Projects();
      projects.setAbout(about);
      projects.setName(name);
      projects.setUid(uid);
      if (fzrid>-1){
        projects.setFuzeren(fzrid);//添加项目负责人
      }
      if (StrKit.notBlank(lead_time)) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        try {
          Date date = null;
          date = sdf.parse(lead_time);
          projects.setLeadTime(date);
          result = projects.save();
        } catch (ParseException e) {
          e.printStackTrace();
        }
      }
    }

    if (result) {
      renderJson(Https.success("添加成功", ""));
    } else {
      renderJson(Https.failure("添加失败", ""));
    }

  }

  /**
   * 修改项目
   */
  public void set() {
    String name = getPara("name", "");
    String lead_time = getPara("lead_time", "");
    String about = getPara("about", "");
    final int pid = getParaToInt("pid", -1);
    final int fzrid = getParaToInt("fzrid", -1);
    boolean result = false;
    if (StrKit.notBlank(name) && pid > -1) {
      Projects projects = Projects.dao.findById(pid);
      if (projects != null) {
        if (StrKit.notBlank(about)) {
          projects.setAbout(about);
        }
        if (StrKit.notBlank(name)) {
          projects.setName(name);
        }
        if (fzrid>-1){
          projects.setFuzeren(fzrid);//添加项目负责人
        }
        if (StrKit.notBlank(lead_time)) {
          SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
          try {
            Date date = null;
            date = sdf.parse(lead_time);
            projects.setLeadTime(date);
            result = projects.update();
          } catch (ParseException e) {
            e.printStackTrace();
          }
        }
      }

    }

    if (result) {
      renderJson(Https.success("修改成功", ""));
    } else {
      renderJson(Https.failure("修改失败", ""));
    }
  }

  /**
   * 查询所有项目
   */
  public void getList() {
    final int uid = getParaToInt("uid", -1);
    final String pname = getPara("pname", "");
    final String odtime = getPara("odtime", "");
    final int od = getParaToInt("od", -1);
    final int state = getParaToInt("state", -1);
    final int page = getParaToInt("page", 1);
    final int size = getParaToInt("size", PropKit.getInt("paginate_size", 10));
    Page<Record> list = null;
    list = Projects.getList(pname, state, uid, odtime, od, page, size);

    renderJson(Https.success("查询成功", list));

  }

  public void getListPname() {
    List<Projects> list = null;
    list = Projects.dao.findAll();
    renderJson(Https.success("查询成功", list));
  }
}
